#include <vector>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
/*
 * @lc app=leetcode.cn id=40 lang=cpp
 *
 * [40] 组合总和 II
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> res;
    // 1 1 2 5 6 7 10
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<int> n;
        sort(candidates.begin(), candidates.end());
        dfs(n, candidates, target, 0);
        return res;
    }

    void dfs(vector<int> & now, vector<int>& candidates, int target, int index) {
        if (target == 0) {
            res.push_back(now);
            return;
        }
        if (index >= candidates.size()) return;

        for (int i = index; i < candidates.size(); i++) {
            if (target < candidates[i]) break;
            if (i != index && candidates[i] == candidates[i - 1]) continue;
            now.push_back(candidates[i]);
            dfs(now, candidates, target - candidates[i], i + 1);
            now.pop_back();
        }
    }
};
// @lc code=end

